#!/bin/bash

# Slackware build script for cosmic_assault

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20230107 bkw: BUILD=3, fix icon size.
# 20211228 bkw: BUILD=2.
# - convert .mod music to .ogg, since -current's SDL2_mixer (and thus
#   pygame) utterly lack MOD music support.
# - use pygame.SCALED to scale the game to the desktop resolution.
# - new-style icon.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=cosmic_assault
VERSION=${VERSION:-1.1}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

ARCH=noarch

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

SRCNAM=Cosmic_Assault

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $PRGNAM
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# 20211228 bkw: this makes the game behave better as a fullscreen app.
sed -i '/winstyle/s,FULLSCREEN,pygame.SCALED | pygame.FULLSCREEN,' $PRGNAM.py

# 20211228 bkw: no tracker music support in SDL2_mixer (yet?). This
# workaround is OK, but it bloats the package by 500% (from ~1MB to
# ~5MB, which is still not so large by modern standards).
sed -i \
    -e 's,jene\.mod,jene.ogg,' \
    -e 's,beyond\.s3m,beyond.ogg,' \
    $PRGNAM.py
timidity -Ow1sl -id -o- data/jene.mod   | oggenc -Q -b 64 -o data/jene.ogg -
timidity -Ow1sl -id -o- data/beyond.s3m | oggenc -Q -b 64 -o data/beyond.ogg -
rm -f data/jene.mod data/beyond.s3m

mkdir -p $PKG/usr/games $PKG/usr/share/games/$PRGNAM
install -m0755 $PRGNAM.py $PKG/usr/share/games/$PRGNAM
install -m0755 $CWD/$PRGNAM.sh $PKG/usr/games/$PRGNAM
cp -a data $PKG/usr/share/games/$PRGNAM

# icon made from game data image:
# convert -scale 64x64 data/newship.gif cosmic_assault.png
# ...and then fixed up with the gimp :(
mkdir -p $PKG/usr/share/icons/hicolor/64x64/apps $PKG/usr/share/pixmaps
cat $CWD/$PRGNAM.png > $PKG/usr/share/icons/hicolor/64x64/apps/$PRGNAM.png
ln -s ../icons/hicolor/64x64/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# .desktop written for this build
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# no docs at all, include our own README
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/README > $PKG/usr/doc/$PRGNAM-$VERSION/README
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
